package com.example.mttab;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.shape.view.ShapeImageView;
import com.hjq.shape.view.ShapeTextView;


public class MMBottomNavItemView extends FrameLayout {
    private MMBottomNavView.MMBottomNavDataBean mmBottomNavDataBean;
    private TabAnimView tavCircle;
    private ImageView imgRes;
    private ShapeImageView imgResBig;
    private TextView tvName;
    private ShapeTextView stvMegNum;

    public MMBottomNavItemView(@NonNull Context context) {
        this(context,null);
    }

    public MMBottomNavItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MMBottomNavItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.view_mm_bottom_nav_item,this);
        tavCircle = (TabAnimView) findViewById(R.id.tav_circle);
        imgRes = (ImageView) findViewById(R.id.img_res);
        imgResBig = (ShapeImageView) findViewById(R.id.img_res_big);
        tvName = (TextView) findViewById(R.id.tv_name);
        stvMegNum = (ShapeTextView) findViewById(R.id.stv_meg_num);
    }

    public void setData(MMBottomNavView.MMBottomNavDataBean mmBottomNavDataBean){
        this.mmBottomNavDataBean = mmBottomNavDataBean;
        tvName.setText(mmBottomNavDataBean.getName());
        imgRes.setImageResource(mmBottomNavDataBean.getDefaultImageRes());
        imgResBig.setImageResource(mmBottomNavDataBean.getSelectImageRes());
    }

    public void setMsgNum(int num){
        if(num <= 0){
            stvMegNum.setVisibility(GONE);
        }else {
            stvMegNum.setText(num + "");
            stvMegNum.setVisibility(VISIBLE);
        }
    }

    public void selectItem(boolean select){
        if(select){
            tvName.setTextColor(mmBottomNavDataBean.getSelectTextColor());
            imgResBig.setVisibility(VISIBLE);
            imgRes.setVisibility(GONE);

            Animation scaleAnimation = new ScaleAnimation(0.6f, 1f, 0.6f, 1f, ScaleAnimation.RELATIVE_TO_SELF,
                    0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setDuration(100);
            AnimationSet animationSet = new AnimationSet(true);
            animationSet.addAnimation(scaleAnimation);
            animationSet.setDuration(100);
            animationSet.setFillAfter(false);
            imgResBig.startAnimation(animationSet);
//            imgRes.setImageResource(mmBottomNavDataBean.getSelectImageRes());
//            imgRes.animate().setDuration(100).scaleX(1.5f).scaleY(1.5f).translationY(-10).start();
            tavCircle.startAnim();
        }else {
            tvName.setTextColor(mmBottomNavDataBean.getDefaultTextColor());
            imgResBig.setVisibility(GONE);
            imgRes.setVisibility(VISIBLE);
//            imgRes.setImageResource(mmBottomNavDataBean.getDefaultImageRes());
//            imgRes.animate().setDuration(100).scaleX(1f).scaleY(1f).translationY(10).start();
            tavCircle.resetAnim();
        }
    }
}
